// 引入共用样式表
require('../../assets/css/index.js')
require('../../assets/fonts/font/iconfont.css')
require('../../assets/lib/swiper/swiper-bundle.css')
let Swiper = require('../../assets/lib/swiper/swiper-bundle.js')
require('../../utils/local.js')
let https = require('../../utils/https.js')
// 引入公共js
let dom = require('../../utils/dom.js')
const local = require('../../utils/local.js')


//引入当前页面的less文件
require('./home.less')
// 当前页面js
dom.isLogin()

dom.ready(function () {
    dom.bMenu('home')
    dom.getEle(".sport").addEventListener("click",function(){
        window.location.href="sportsData.html"
    })
    dom.getEle(".badge").addEventListener("click",function(){
        window.location.href="badge.html"
    })
    dom.getEle(".outside").addEventListener("click",function(){
        window.location.href="sports.html"
    })
    new Swiper('.swiper', {
        // direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项
        autoplay: true,

        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

        // 如果需要前进后退按钮
        // navigation: {
        //   nextEl: '.swiper-button-next',
        //   prevEl: '.swiper-button-prev',
        // },

        // 如果需要滚动条
        // scrollbar: {
        //   el: '.swiper-scrollbar',
        // },
    })
    //   获取个人信息,进行页面数据渲染
    function getUserInfo() {
        https.get('/api/user/info', {}, function (res) {
            let { errno, data } = res;
            if (errno == 0) {
                console.log(data);
                // 把用户数据存本地
                local.set('userInfo', data);
                // 动态数据到页面
                dom.getEle('#rankTxt').innerHTML = data.ranking;
                dom.getEle('#clockTxt').innerHTML = data.clockCount;
                dom.getEle('#badgesTxt').innerHTML = data.badges;

            }
        })

    }
    getUserInfo()
    // 获取用户打卡情况
    function getClockstatus() {
        https.get('/api/user/clockInInfo', {}, function (res) {
            // console.log(res);
            let { errno, data } = res;
            if (errno == 0) {
                
                dom.getEle('#clockTxt').innerHTML = data.clockCount;
                dom.getEle('#clock').innerHTML = data.isClockIn ? '已经打卡' : '今日打卡';

            }
        })

    }
    // 网页加载时调用
    getClockstatus()
    // 打卡
    dom.getEle('#clock').addEventListener("click", function () {
        https.post('/api/user/clockIn', {}, function (res) {
            console.log(res);
            
            let { errno, data} = res;
            // console.log(res);
            if (errno == 0) {
                alert("打卡成功");
                // 打卡后动态渲染
                getClockstatus()

            }else if(errno==-1){
                alert(res.message)
            }
        })
    })
})